package com.xiao.sulinges.service.imp;

import com.alibaba.fastjson.JSON;
import com.xiao.sulinges.config.ElasticSearchConfig;
import com.xiao.sulinges.service.ShopEsService;
import com.xiao.sulingpublic.myutils.MyUtils;
import com.xiao.sulingpublic.to.BusinessTo;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/22 14:17
 * @作用:
 */
@Service("shopEsService")
public class ShopEsServiceImpl implements ShopEsService {
    private static final String index = "suling_shop";
    /**
     * 操作es用的
     */
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public void save(BusinessTo businessTo) throws IOException {
        IndexRequest indexRequest = new IndexRequest(index);
        indexRequest.id(String.valueOf(businessTo.getBuMemberUsername()));

        String s = JSON.toJSONString(businessTo);
        indexRequest.source(s, XContentType.JSON);

        restHighLevelClient.index(indexRequest, ElasticSearchConfig.COMMON_OPTIONS);
    }

    @Override
    public List<BusinessTo> list(BigDecimal longitude, BigDecimal latitude, Integer distince) throws IOException {
        //1.获取附近经纬范围 String 格式：经度最小值-经度最大值-纬度最小值-纬度最大值  minlng + "-" + maxlng + "-" + minlat + "-" + maxlat;
        String rangeByLatLon = MyUtils.getRangeByLatLon(longitude, latitude, distince);
        String[] split = rangeByLatLon.split("-");

        //2.构建检索条件
        SearchSourceBuilder searchSource = new SearchSourceBuilder();

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        /* 按经纬度 查询在这个范围中的店铺 */
        boolQuery.must(QueryBuilders.rangeQuery("buLatitude").gte(split[2]).lte(split[3]));
        boolQuery.filter(QueryBuilders.rangeQuery("buLongitude").gte(split[0]).lte(split[1]));

        searchSource.query(boolQuery);
        SearchRequest searchRequest = new SearchRequest(new String[]{index}, searchSource);

        //执行检索条件
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //获取返回的数据
        List<BusinessTo> businessTos = new ArrayList<>();
        if(0 != search.getHits().getTotalHits().value){
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                // 店铺的数据
                String sourceAsString = hit.getSourceAsString();
                BusinessTo businessTo = JSON.parseObject(sourceAsString, BusinessTo.class);

                businessTos.add(businessTo);
            }
        }

        return businessTos;
    }
}
